package com.dimeng.adapter;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.dimeng.R;
import com.dimeng.entity.ProductSearchViewCache;
import com.dimeng.entity.ResultEntity;
import com.dimeng.utils.AsyncImageLoader;
import com.dimeng.utils.AsyncImageLoader.ImageCallback;
import com.dimeng.utils.CommonUtil;

/**
 * @author write by ccn<br>
 *         <b>helloandroid100@gmail.com</b>
 * @version 0.1;2012-5-18 下午5:30:44<br>
 *          descrition:产品搜索结果的数据适配器
 */
public class ProdSearchResultAdapter extends ArrayAdapter<ResultEntity> {
	private AsyncImageLoader asyncImageLoader = AsyncImageLoader.getInstance();
	private ListView listView;

	public ProdSearchResultAdapter(Context context,
			List<ResultEntity> prodSearchResults, ListView listView) {
		super(context, 0, prodSearchResults);
		this.listView = listView;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		Activity activity = (Activity) getContext();
		// Inflate the views from XML
		View prodRowView = convertView;
		ResultEntity searchResult = getItem(position);
		ProductSearchViewCache productSearchViewCache;
		if (prodRowView == null) {
			LayoutInflater inflater = activity.getLayoutInflater();
			prodRowView = inflater.inflate(R.layout.prod_search_list_item, null);
			productSearchViewCache = new ProductSearchViewCache(prodRowView);
			prodRowView.setTag(productSearchViewCache);
		} else {
			productSearchViewCache = (ProductSearchViewCache) prodRowView
					.getTag();
		}
		// Load the image and set it on the ImageView
		String imageUrl = CommonUtil.getResPath(searchResult.getImageUrl(),"M");
		ImageView imageView = productSearchViewCache.getImageView();
		imageView.setTag(imageUrl);
		Drawable cachedImage = asyncImageLoader.loadDrawable(imageUrl,
				new ImageCallback() {
					public void imageLoaded(Drawable imageDrawable,
							String imageUrl) {
						ImageView imageViewByTag = (ImageView) listView
								.findViewWithTag(imageUrl);
						if (imageViewByTag != null) {
							imageViewByTag.setImageDrawable(imageDrawable);
						}
					}
				});
		imageView.setImageDrawable(cachedImage);

		// Set the text on the TextView
		TextView prodNameTextView = productSearchViewCache.getProdNameView();
		prodNameTextView.setText(Html.fromHtml(searchResult.getProdName()));
		TextView regionTextView = productSearchViewCache.getRegionView();
		regionTextView.setText(searchResult.getRegion());
		TextView publishTiemView = productSearchViewCache.getPublishTiemView();
		publishTiemView.setText(searchResult.getRegisterOrPublishTime());
		TextView companyNameView = productSearchViewCache.getCompanyNameView();
		companyNameView.setText(searchResult.getCompanyName());
		return prodRowView;
	}
}
